

function initChart(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr
  });
  canvas.setChart(chart);

  const option = {
    tooltip: {
      trigger: 'axis',
      formatter: '{b}: {c}bmp'
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      top: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['1/4', '1/24', '1/31', '2/4', '2/14', '2/18', '3/1'],
      axisLine: {
        lineStyle: {
          color: '#999'
        }
      },
      axisLabel: {
        color: '#666'
      }
    },
    yAxis: {
      type: 'value',
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      splitLine: {
        lineStyle: {
          color: '#eee',
          type: 'dashed'
        }
      },
      axisLabel: {
        color: '#666'
      }
    },
    series: [{
      data: [60, 66, 70, 90, 106, 92, 74],
      type: 'line',
      smooth: true,
      symbol: 'circle',
      symbolSize: 8,
      itemStyle: {
        color: '#FF69B4'
      },
      lineStyle: {
        color: '#FF69B4',
        width: 3
      },
      areaStyle: {
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [{
            offset: 0,
            color: 'rgba(255,105,180,0.3)'
          }, {
            offset: 1,
            color: 'rgba(255,105,180,0)'
          }]
        }
      }
    }]
  };

  chart.setOption(option);
  return chart;
}
import request from '../../../../utils/request'
Page({
  data: {
    ec: {
      onInit: initChart
    },
    currentDate: '2021年3月1日 14:00',
    heartrate: '90',
    heartrateStatus: '正常',
    showModal: false,
    inputHeartrate: '',
    heartrateRecords: [
      {
        id: 1,
        date: '01/24',
        time: '2020',
        value: '80',
        status: '正常'
      }
    ]
  },

  onLoad: function() {
    this.getHeartRate()
    .then(heartrate=>{
        this.setData({
            heartrate:heartrate
        })
    })
  },
 //获取用户心率
 getHeartRate(){
    return new Promise((resolve,reject)=>{
        let userId = wx.getStorageSync('userid')
        let heartrateid = 4  //数据库中心率id=4
        let url = `healthlog/healthlog/selectByUseridAndTypeid?userid=${userId}&typeid=${heartrateid}`
        request({
            url:url,
            method:'GET',
            success(res){
                // console.log("体重列表：",res.data);
                let heartrate = res.data
                resolve(heartrate)
            },fail(error){
                reject(error)
            }
        })
    })
  },
  goBack: function() {
    wx.navigateBack({
      delta: 1
    });
  },

  showHeartrateModal: function() {
    this.setData({
      showModal: true,
      inputHeartrate: ''
    });
  },

  hideModal: function() {
    this.setData({
      showModal: false
    });
  },

  onHeartrateInput: function(e) {
    this.setData({
      inputHeartrate: e.detail.value
    });
  },

  getHeartrateStatus: function(value) {
    if (value < 60) return '偏低';
    if (value > 100) return '偏高';
    return '正常';
  },

  saveHeartrate: function() {
    if (!this.data.inputHeartrate) {
      wx.showToast({
        title: '请输入心率值',
        icon: 'none'
      });
      return;
    }

    const heartrate = parseInt(this.data.inputHeartrate);
     // 获取当前时间并格式化为 YYYY-MM-DD HH:mm:ss 的形式
     const now = new Date();
     const year = now.getFullYear();
     const month = String(now.getMonth() + 1).padStart(2, '0');
     const day = String(now.getDate()).padStart(2, '0');
     const hours = String(now.getHours()).padStart(2, '0');
     const minutes = String(now.getMinutes()).padStart(2, '0');
     const seconds = String(now.getSeconds()).padStart(2, '0');
     const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

     // 从本地存储中获取当前用户的 ID
     const userId = wx.getStorageSync('userid');

     // 构建要插入数据库的心率记录对象
     const record = {
         number: heartrate, // 心率数值
         typeid: 4, // 心率类型 ID，固定值为 3
         time: formattedTime, // 当前时间
         source: 1, // source 字段，固定值为 1
         userId: userId // 当前用户的 ID
     };

     // 定义数据库插入接口的 URL
     let url = `healthlog/healthlog`;
     // 发送 POST 请求将数据插入数据库
     request({
         url: url,
         method: 'POST',
         data: record,
         success: (res) => {
             // 插入成功时的处理逻辑
             console.log("心率数据插入成功", res.data);
             wx.showToast({
                 title: '保存成功',
                 icon: 'success'
             });
             // 插入成功后重新获取心率列表
             this.getHeartRate()
                 .then(heartrate => {
                     this.setData({
                        heartrate: heartrate
                     });
                     // 获取成功后关闭弹窗并清空输入框
                     this.setData({
                         showModal: false,
                         inputSystolic: ''
                     });
                 })
                 .catch(error => {
                     console.error("获取心率列表失败", error);
                     wx.showToast({
                         title: '获取心率数据失败',
                         icon: 'none'
                     });
                 });
         },
         fail: (error) => {
             // 插入失败时的处理逻辑
             console.error("心率数据插入失败", error);
             wx.showToast({
                 title: '保存失败',
                 icon: 'none'
             });
         }

     });
  },

  editHeartrate: function(e) {
    const id = e.currentTarget.dataset.id;
    const record = this.data.heartrateRecords.find(item => item.id === id);
    if (record) {
      this.setData({
        showModal: true,
        inputHeartrate: record.value
      });
    }
  },

  deleteHeartrate: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除这条记录吗？',
      success: res => {
        if (res.confirm) {
          const heartrateRecords = this.data.heartrateRecords.filter(item => item.id !== id);
          this.setData({
            heartrateRecords
          });
          wx.showToast({
            title: '删除成功',
            icon: 'success'
          });
        }
      }
    });
  }
}); 